# coding: utf-8
# author: ZhenbPeng
#   date: 2017/10/10 10:19
import time
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.proxy import ProxyType
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities


START_URL = 'http://www.ttplus.cn/publish/app/data/2017/10/19/102689/share1.html'
proxy = Proxy(
    {
        'proxyType': ProxyType.MANUAL,
        'httpProxy': '192.168.10.222:8888'  # 代理ip和端口
    }
)
# headers = {
#     'Accept': '*/*',
#     'Accept-Language': 'en-US,en;q=0.8',
#     'Cache-Control': 'max-age=0',
#     'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36',#这种修改 UA 也有效
#     'Connection': 'keep-alive',
# }

desired_capabilities = DesiredCapabilities.PHANTOMJS.copy()
desired_capabilities['phantomjs.page.customHeaders.User-Agent'] ='Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Mobile/14G60 MicroMessenger/6.5.9 NetType/WIFI Language/zh_CN'

proxy.add_to_capabilities(desired_capabilities)
driver = webdriver.PhantomJS(desired_capabilities=desired_capabilities)
driver.set_page_load_timeout(60)
driver.set_script_timeout(60)
driver.get(START_URL)


# driver.find_element_by_xpath('//li[@class="newsList-item"]')
# time.sleep(1)

# driver.execute_script("var q=document.documentElement.scrollTop=10000")
oldtile = driver.title
driver.execute_script("document.getElementById('newsDetailAd').getElementsByTagName('img')[0].click();")
time.sleep(3)
newtitle = driver.title
if oldtile != newtitle:
    print("success click ad!")

# driver.find_element_by_xpath('//div[@class="newsDetailAd"][@id="newsDetailAd"]/div/div[@iss="1"]/a').click()
driver.close()
# driver.quit()

driver.get("http://resource.ttplus.cn/h5/ttplus-m/share0.html")
driver.close()
